<?php
namespace app\logic;


use app\models\Cancel;
use app\models\Config;
use app\models\Income;
use yii\db\Query;


class MoneyLogic
{
    public function getUserIncome($user_id){

    }

    public function getCommissionConfig(){
        return Config::find()->where(['config_name'=>'commission'])->asArray()->all();
    }

    public function getCancelListAndIncomeByMyself($user_id,$shop_ids,$type){
        if($type==1){
            return Cancel::find()
                ->select('c.*,o.*,u.phone,p.*,g.*,c.type as cancel_type, o.type as order_type,o.price as price,i.*')
                ->from('{{%cancel}} c')
                ->leftJoin('{{%order_cancel}} oc', 'oc.cancel_id=c.cancel_id')
                ->leftJoin('{{%order}} o','o.order_id=oc.order_id')
                ->leftJoin('{{%play}} p','p.play_id=o.gp_id AND o.type=0')
                ->leftJoin('{{%goods}} g','g.goods_id=o.gp_id  AND o.type=1')
                ->leftJoin('{{%user}} u','u.user_id=o.user_id')
                ->leftJoin('{{%income}} i','i.cancel_id=c.cancel_id and i.type=1')
                ->where(['c.shop_id'=>$shop_ids])
                ->asArray()
                ->all();
        }else{
            return Cancel::find()
                ->select('c.*,o.*,u.phone,p.*,g.*,c.type as cancel_type, o.type as order_type,o.price as price,i.*')
                ->from('{{%cancel}} c')
                ->leftJoin('{{%order_cancel}} oc', 'oc.cancel_id=c.cancel_id')
                ->leftJoin('{{%order}} o','o.order_id=oc.order_id')
                ->leftJoin('{{%play}} p','p.play_id=o.gp_id AND o.type=0')
                ->leftJoin('{{%goods}} g','g.goods_id=o.gp_id  AND o.type=1')
                ->leftJoin('{{%user}} u','u.user_id=o.user_id')
                ->leftJoin('{{%manager}} m','m.manager_id=c.manager_id')
                ->leftJoin('{{%income}} i','i.cancel_id=c.cancel_id and i.type=2')
                ->where("m.user_id=$user_id")
                ->asArray()
                ->all();
        }

    }

    public function getDefaultConfig(){
        return Config::find()->where(['config_name'=>'commission','plan_id'=>1])->asArray()->all();
    }

    public function getConfig($plan_id){
        return Config::find()->where(['config_name'=>'commission','plan_id'=>$plan_id])->asArray()->all();
    }

    public function getPlan(){
        return Config::find()->select("plan_id")->where(['config_name'=>'commission'])->groupBy('plan_id')->asArray()->all();
    }
    public function getMaxPlan(){
        return Config::find()->select("plan_id")->where(['config_name'=>'commission'])->groupBy('plan_id')->orderBy('plan_id desc')->asArray()->one();
    }

    public function addConfig($key,$value,$id){
        $config = new Config();
        $config->config_name = "commission";
        $config->config_value = $value;
        $arr = explode(',',$key);
        $config->config_ext = json_encode(['role'=>(int)$arr[0],'type'=>(int)$arr[1]]);
        $config->plan_id = $id;
        return $config->save();
    }


    public function getConfigById($config_id){
        return Config::findOne($config_id)->toArray();
    }

    public function editConfigById($id,$params){
        $config = Config::findOne($id);
        $config->config_value = $params['config_value'];
        return $config->save();
    }

    public function getCommission($start_time,$end_time,$type,$params){

        $query = Income::find()
            ->select('i.user_id,c.shop_id,s.shop_name,sum(i.commission) as income,u.phone,u.type as type,a4.region_name as province,a1.region_name as city,a2.region_name as district,a3.region_name as street')
            ->from('{{%income}} i')
            ->leftJoin('{{%cancel}} c','c.cancel_id=i.cancel_id')
            ->leftJoin('{{%user}} u','i.user_id=u.user_id')
            ->leftJoin('{{%shop}} s','s.shop_id=c.shop_id')
            ->leftJoin('{{%area}} a4','a4.region_code=s.province_id')
            ->leftJoin('{{%area}} a1','a1.region_code=s.city_id')
            ->leftJoin('{{%area}} a2','a2.region_code=s.district_id')
            ->leftJoin('{{%area}} a3','a3.region_code=s.street_id')
            ->where("c.type=$type AND income_time>='$start_time' AND income_time<='$end_time'");
        if(isset($params['shop_id']) && $params['shop_id']!=0){
            $query->andWhere(['s.shop_id'=>$params['shop_id']]);
        }

        if(isset($params['province_id']) && isset($params['city_id']) && isset($params['district_id']) && isset($params['street_id'])){
            if($params['province_id']!=0) {
                $query->andWhere(['s.province_id' => $params['province_id']]);
            }
            if($params['city_id']!=0){
                $query->andWhere(['s.city_id'=>$params['city_id']]);
            }
            if($params['district_id']!=0){
                $query->andWhere(['s.district_id'=>$params['district_id']]);
            }
            if($params['street_id']!=0){
                $query->andWhere(['s.street_id'=>$params['street_id']]);
            }
        }


        return $query->groupBy('c.shop_id,i.user_id')
            ->asArray()
            ->all();

    }


}